package application;

import java.io.File;

import data.CityArea;
import fetch.paginegialle.Fetcher;

import utils.config.Config;

public class MainFetcher {
	private String OUT_DIR;
	private CityArea AREA;
	private String URL;
	
	public static void main(String[] args) throws Exception{
		
		MainFetcher mf = new MainFetcher();
		mf.fetch();
		
		System.out.println("Done");
	}
	
	public MainFetcher(){
		Config config = new Config(this.getClass().getSimpleName()+".ser");
		
		OUT_DIR = (String)config.get("OUT_DIR");
		
		AREA = new CityArea((String)config.get("AREA_NAME"), 
				   (double[][])config.get("AREA_BBOX"),
				   (String[])config.get("BASE_IN_DIRS"));
		
		URL = (String)config.get("URL");
		
	}
	
	public void fetch() throws Exception{
		
		//create DIR
		new File(OUT_DIR+"/"+AREA.getName()+"/").mkdir();
		
		System.out.println(AREA.getNrows()+" rows of data to be processed");
		
		Fetcher f = new Fetcher();
		
		for(int i=0;i<AREA.getNrows();i++){
			for(int j=0;j<AREA.getNcols();j++){
				double [] c = AREA.getCenter(i, j);
				System.out.println(c[0]+"-"+c[1]);
				double radius = AREA.getRadius();
				System.out.println(radius);
				
				String json_file = OUT_DIR+"/"+AREA.getName()+"/"+c[1]+"-"+c[0]+"-"+radius+".json";
				
				String query = URL+"?lat="+c[1]+"&lon="+c[0]+"&range="+radius+"&application=mrtyp";
				
				f.fetch(query, json_file);
				
				Thread.sleep(1000);
			}
			
			System.out.println((i+1)+" out of "+AREA.getNrows()+" rows completed!");
		}

	}

}
